home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / oleo-1_4.lha / oleo-1.4 / line.c < prev    next >
C/C++ Source or Header  |  1993-05-23  |  4KB  |  235 lines

  1. #include <stdio.h>
  2. #include "global.h"
  3. #include "line.h"
  4.  
  5. #ifdef __STDC__
  6. void
  7. set_line (struct line *line, char *string)
  8. #else
  9. void
  10. set_line (line, string)
  11.      struct line *line;
  12.      char *string;
  13. #endif
  14. {
  15.   int len;
  16.  
  17.   len = strlen (string);
  18.   if (line->alloc <= len)
  19.     {
  20.       if (len < LINE_MIN)
  21.     len = LINE_MIN;
  22.       else
  23.     len++;
  24.       line->alloc = len + 1;
  25.       if (line->buf)
  26.     line->buf = ck_realloc (line->buf, line->alloc);
  27.       else
  28.     line->buf = ck_malloc (line->alloc);
  29.     }
  30.   strcpy (line->buf, string);
  31. }
  32.  
  33. #ifdef __STDC__
  34. void
  35. setn_line (struct line *line, char *string, int n)
  36. #else
  37. void
  38. setn_line (line, string, n)
  39.      struct line *line;
  40.      char *string;
  41.      int n;
  42. #endif
  43. {
  44.   int len = n;
  45.   if (line->alloc <= len)
  46.     {
  47.       if (len < LINE_MIN)
  48.     len = LINE_MIN;
  49.       else
  50.     len++;
  51.       line->alloc = len;
  52.       line->buf = ck_remalloc (line->buf, line->alloc);
  53.     }
  54.   bcopy (string, line->buf, n);
  55.   line->buf[n] = 0;
  56. }
  57.  
  58. #define Max(A,B)  ((A) > (B) ? (A) : (B))
  59.  
  60. #ifdef __STDC__
  61. void
  62. catn_line (struct line *line, char *string, int n)
  63. #else
  64. void
  65. catn_line (line, string, n)
  66.      struct line *line;
  67.      char *string;
  68.      int n;
  69. #endif
  70. {
  71.   int len = (line->buf ? strlen (line->buf) : 0);
  72.   if (line->alloc <= len + n + 1)
  73.     {
  74.       line->alloc = Max (len + n + 1, LINE_MIN);
  75.       line->buf = ck_remalloc (line->buf, line->alloc);
  76.     }
  77.   if (n)
  78.     bcopy (string, line->buf + len, n);
  79.   line->buf[len + n] = '\0';
  80. }
  81.  
  82.  
  83. #ifdef __STDC__
  84. void
  85. sprint_line (struct line *line, char * fmt, ...)
  86. #else
  87. void
  88. sprint_line (line, fmt, va_alist)
  89.      struct line *line;
  90.      char *fmt;
  91.      va_dcl
  92. #endif
  93. {
  94.   va_list iggy;
  95.   int len;
  96.  
  97.   len = strlen (fmt) + 200;
  98.   if (!line->alloc)
  99.     {
  100.       line->buf = ck_malloc (len);
  101.       line->alloc = len;
  102.     }
  103.   else if (line->alloc < len)
  104.     {
  105.       line->buf = ck_realloc (line->buf, len);
  106.       line->alloc = len;
  107.     }
  108.   var_start (iggy, fmt);
  109.   vsprintf (line->buf, fmt, iggy);
  110.   va_end (iggy);
  111. }
  112.  
  113. #ifdef __STDC__
  114. void
  115. splicen_line (struct line * line, char * str, int n, int pos)
  116. #else
  117. void
  118. splicen_line (line, str, n, pos)
  119.      struct line * line;
  120.      char * str;
  121.      int n;
  122.      int pos;
  123. #endif
  124. {
  125.   int old_len = strlen (line->buf);
  126.   int len = old_len + n;
  127.   if (line->alloc <= len)
  128.     {
  129.       line->alloc = len;
  130.       line->buf = ck_remalloc (line->buf, len + 1);
  131.     }
  132.   line->buf[len--] = '\0';
  133.   --old_len;
  134.   while (old_len >= pos)
  135.     {
  136.       line->buf[len] = line->buf[old_len];
  137.       --len;
  138.       --old_len;
  139.     }
  140.   while (n--)
  141.     line->buf[pos + n] = str[n];
  142. }
  143.  
  144. #ifdef __STDC__
  145. void
  146. edit_line (struct line * line, int begin, int len)
  147. #else
  148. void
  149. edit_line (line, begin, len)
  150.      struct line * line;
  151.      int begin;
  152.      int len;
  153. #endif
  154. {
  155.   int old_len = strlen (line->buf);
  156.   int new_len = old_len - len;
  157.   while (begin < new_len)
  158.     {
  159.       line->buf[begin] = line->buf[begin + len];
  160.       ++begin;
  161.     }
  162.   line->buf[begin] = '\0';
  163. }
  164.  
  165.  
  166. #ifdef __STDC__
  167. void
  168. free_line (struct line * line)
  169. #else
  170. void
  171. free_line (line)
  172.      struct line * line;
  173. #endif
  174. {
  175.   if (line->buf && line->alloc)
  176.     free (line->buf);
  177.   line->buf = 0;
  178.   line->alloc = 0;
  179. }
  180.  
  181.  
  182.  
  183.  
  184. #ifdef __STDC__
  185. int
  186. read_line (struct line * line, FILE * fp, int * linec)
  187. #else
  188. int
  189. read_line (line, fp, linec)
  190.      struct line * line;
  191.      FILE * fp;
  192.      int * linec;
  193. #endif
  194. {
  195.   int pos = 0;
  196.   int c = getc (fp);
  197.  
  198.   while ((c != EOF) && (c != '\n'))
  199.     {
  200.       if (pos + 2 >= line->alloc)
  201.     {
  202.       line->alloc = (line->alloc ? line->alloc * 2 : 1);
  203.       line->buf = ck_remalloc (line->buf, line->alloc);
  204.     }
  205.       if (c != '\\')
  206.     line->buf[pos++] = c;
  207.       else
  208.     {
  209.       int next_c = getc (fp);
  210.       if (next_c != '\n')
  211.         {
  212.           line->buf[pos++] = '\\';
  213.           line->buf[pos++] = next_c;
  214.         }
  215.       /* Else the backslash and newline are discarded from the input. */
  216.       else
  217.         ++*linec;
  218.     }
  219.       c = getc (fp);
  220.     }
  221.   if (pos + 1 > line->alloc)
  222.     {
  223.       ++line->alloc;
  224.       line->buf = ck_remalloc (line->buf, line->alloc);
  225.     }
  226.   line->buf[pos] = 0;
  227.   if (line->buf[0] || (c != EOF))
  228.     {
  229.       ++*linec;
  230.       return 1;
  231.     }
  232.   else
  233.     return 0;
  234. }
  235.